using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.ProvinceManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;
using Business.WarehouseManagement.Dto;
using Volo.Abp.Collections;

namespace Business.ProvinceManagement
{
    [Authorize(BusinessPermissions.Province.Default)]
    public class ProvinceAppService : ApplicationService,IProvinceAppService
    {
        private const string FormName = "Province";
        private IRepository<Province, Guid> _repository;
        private IRepository<Shi, Guid> _shirepository;
        private IRepository<Region, Guid> _regionrepository;

        public ProvinceAppService(
            IRepository<Province, Guid> repository, IRepository<Shi, Guid> shirepository, IRepository<Region, Guid> regionrepository
            )
        {
            _repository = repository;
            _shirepository= shirepository;
            _regionrepository= regionrepository;
        }
        #region 增删改查基础方法

        public async Task<ProvinceDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Province, ProvinceDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<ProvinceDto>> GetAll(GetProvinceInputDto input)
        {
            var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.ProvinceName.Contains(input.Filter));

            var totalCount = await query.CountAsync();
            var items = await query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount)
                       .Take(input.MaxResultCount)
                       .ToListAsync();

            var dto = ObjectMapper.Map<List<Province>, List<ProvinceDto>>(items);
            return new PagedResultDto<ProvinceDto>(totalCount, dto);
        }

        public async Task<ProvinceDto> DataPost(CreateOrUpdateProvinceDto input)
        {
            Province result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateProvinceDto, Province>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Province, ProvinceDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

        public async Task<TreeTypeDto> GetTreeProvince()
        {
            var shenglist=await _repository.GetListAsync();
            var shilist=await _shirepository.GetListAsync();
            var qulist = await _regionrepository.GetListAsync();

            var treelist = (from a in shenglist
                           select new TreeNode
                           {
                               value = a.ProvinceNo,
                               label = a.ProvinceName,
                               children = (from b in shilist
                                           where b.PCode.ToUpper() == a.ProvinceNo.ToUpper()
                                           select new TreeNode
                                           {
                                               value = b.Code,
                                               label=b.ShiName,
                                               children= (from c in qulist
                                                          where c.PCode.ToUpper()==b.Code.ToUpper()
                                                          select new TreeNode
                                                          {
                                                              value = c.RegionNo,
                                                              label=c.RegionName,
                                                          }
                                                          ).ToList(),
                                           }

                                         ).ToList()
                           }).ToList();
            var totalCount = 0;
            var items = treelist;
            return new TreeTypeDto { totalCount = totalCount, items = items };
        }


        #endregion

    }
}